package com.rc.net;

import java.io.File;
import java.net.URL;
import java.util.Date;

import android.util.Log;

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.ResponseHeaderOverrides;
import com.rc.util.Const;

public class UploadFileAmazon {

	public URL uploadfile(File file, String filename) {
		URL url = null;
		try {
			AmazonS3Client s3Client = new AmazonS3Client(
					new BasicAWSCredentials(Const.ACCESS_KEY_AMAZONE,
							Const.ACCESS_KEY_SECRET_KEY));

			PutObjectRequest por = new PutObjectRequest("incomingreceipts",
					filename, file);
			s3Client.putObject(por);
			ResponseHeaderOverrides override = new ResponseHeaderOverrides();
			override.setContentType("image/jpeg");
			GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest(
					"incomingreceipts", filename);
			urlRequest.setExpiration(new Date(
					System.currentTimeMillis() + 3600000));
			urlRequest.setResponseHeaders(override);
			url = s3Client.generatePresignedUrl(urlRequest);
			Log.e("upload amazone", url + "");
		} catch (NoClassDefFoundError e) {
			// TODO: handle exception
			e.printStackTrace();
			Log.e("upload amazone", e.getMessage());
		}
		return url;
	}
}
